﻿
Partial Class Site_Register
    Inherits MyUI.PageBaseApp


    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim key = UI.Page.GetValue("key")

        Me.PnlRegister.Visible = key = ""
        Me.PnlConfirm.Visible = False

        If key <> "" Then
            Dim email = UI.Page.GetValue("email")
            If Utils.MD5.MD5Compare(email, key) Then
                Dim user = DB.TAB.User.Where("email", "=", email).ToObj
                If user IsNot Nothing Then
                    user.Email_Confirm = True
                    DB.SubmitChanges()
                    Me.PnlConfirm.Visible = True
                End If

            End If

            'Response.Redirect("~/site/login.aspx")
        End If

    End Sub

    Protected Sub BtnCreate_Click(sender As Object, e As EventArgs) Handles BtnCreate.Click


        Dim vv As New UI.Validator.ValidatorGroup
        'vv.val.Add(New UI.Validator.ValidatorUIItem(New UI.Validator.Validators.RequiredValidator(Me.TxtDescription.Text), Me.lll))
        vv.val.Add(UI.Validator.UIValidators.Required(Me.TxtEmail, Me.div_EmailConfirm))

        vv.val.Add(UI.Validator.UIValidators.Equal(Me.TxtPassword, Me.TxtConfirmPassword, Me.Div_PassNotMacth))

        vv.val.Add(New UI.Validator.ValidatorUI(
            Function()
                Dim err = Not UI.Validator.DataValidator.Email(Me.TxtEmail.Text) And Me.TxtEmail.Text <> ""
                Me.LblEmailError.Text = "Email non valida"
                Me.LblEmailError.Visible = err
                Return err
            End Function))

        vv.val.Add(New UI.Validator.ValidatorUI(
            Function()
                Dim err = False
                If Me.TxtEmail.Text <> "" Then
                    Dim u = DB.TAB.User.Where("email", "=", Me.TxtEmail.Text).ToObj
                    If u IsNot Nothing Then
                        err = True
                        Me.LblEmailError.Text = "Email già presente"
                    End If
                End If
                Me.LblEmailError.Visible = err
                Return err
            End Function))

        vv.val.Add(New UI.Validator.ValidatorUI(
            Function()
                Dim err = Me.TxtPassword.Text.Length < 8
                Me.div_PassValid.Visible = err
                Return err
            End Function))



        If vv.ValidatorCheck.Count = 0 Then

            Dim PlainID = UI.Page.GetValueInt("p")
            Dim PromoID As Integer = 0
            Dim PlanMonth = 0
            Select Case PlainID
                Case 1
                    PlanMonth = 1
                    PromoID = 1
                Case 2
                    PlanMonth = 1
                    PromoID = 2
                Case 3, 4
                Case Else

                    Response.Redirect("/Site/", True)

            End Select

            '------------------------------------------------
            ' crea lo user
            Dim user = New mDB.User With {
                .Email = Me.TxtEmail.Text, .Name = Me.TxtFirstName.Text, .Surname = Me.TxtLastName.Text, .Password = Me.TxtPassword.Text
            }
            DB.TAB.User.InsertOnSubmit(user)
            DB.SubmitChanges()

            '------------------------------------------------
            ' crea l'ordine
            Dim order = New mDB.User_Order With {
                .User_Id = user.Id, .Amount = 0, .Create_Data = Now, .Plan_Id = PlainID, .Plan_Month = PlanMonth, .Promo_Id = PromoID
            }
            DB.TAB.User_Order.InsertOnSubmit(order)
            DB.SubmitChanges()
            order.Process()

            '------------------------------------------------
            ' invia l'email
            MyApp.Email.System.Queued(MyApp.Email.System.Type.USER_REGISTER, True, New With {.user_id = user.Id})
            'MyApp.Email.Send.User.Register(user)


            '------------------------------------------------
            ' effettua login
            Dim au = New MyUI.AuthUsers()
            au.LogIn(user.Email, user.Password, True)


            '------------------------------------------------
            ' redirect
            Response.Redirect("/Site/Login.aspx")


            ' crea mail

            'MyApp.Task.TemplatePost()



        End If


    End Sub

End Class
